package com.aliang01;

import com.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author Xiao
 * @date 2021/7/7
 */
@SuppressWarnings("all")        //  压制所有警告
public class StudentTest01 {
    /**
     * 学生管理系统
     * ----------欢迎来到学生管理系统----------
     * 1.添加学生
     * 2.删除学生
     * 3.修改学生
     * 4.查看所有学生
     * 5.退出
     * 请输入您的选择：
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();
        String username = "admin";  //用户名
        String password = "123456"; //密码
        System.out.println("----------欢迎来到学生管理系统----------");
        checkUsername(sc, username, password);  //进入登录界面
        menuStudent(sc, list);  //进入选择菜单
    }

    //  登录界面
    public static void checkUsername(Scanner sc, String username, String password) {
        while (true) {
            System.out.println("请输入用户名：");
            String un = sc.nextLine();
            System.out.println("请输入密码：");
            String pw = sc.nextLine();
            if (un.equals(username) && pw.equals(password)) {
                System.out.println("登陆成功！");
                break;
            } else {
                System.out.println("用户名或密码不正确。");
            }
        }
    }

    //  选择菜单
    public static void menuStudent(Scanner sc, ArrayList<Student> list) {
        lo1:
        while (true) {
            System.out.println("----------欢迎来到学生管理系统----------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看所有学生");
            System.out.println("5.退出");
            System.out.println("-----------请输入您的选择(1-5)-----------");
            lo2:
            while (true) {
                int choice = sc.nextInt();
                switch (choice) {
                    case 1:    //  添加学生
                        addStudent(list);
                        break lo2;
                    case 2:    //  删除学生
                        deleteStudent(list);
                        break lo2;
                    case 3:    //  修改学生
                        setStudent(list);
                        break lo2;
                    case 4:    //  查看所有学生
                        lookStudent(list);
                        break lo2;
                    case 5:    //  退出
                        System.out.println("-----------感谢您的使用，再见！-----------");
//                        break lo1;
                        return;
                    default:    //  输入的选项不正确。
                        System.out.println("您输入的选项有误，请重新输入(1-5)：");
                        break;
                }
            }
        }
    }

    //  添加学生    addStudent(list);
    public static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        String sid;
        lo:
        while (true) {
            System.out.println("请输入学生学号(如：001)：");
            sid = "itheima" + sc.next();
            if (sid.length() != 10) {
                System.out.println("您输入的学号有误。");
            } else {
                for (int i = 0; i < list.size(); i++) {
                    Student stu = list.get(i);
                    if (sid.equals(stu.getSid())) {
                        System.out.println("您输入的学号已存在。");
                        continue lo;
                    }
                }
                break;
            }
        }
        System.out.println("请输入学生姓名：");
        String name = sc.next();
        int age;
        while (true) {
            System.out.println("请输入学生年龄(10-20)：");
            age = sc.nextInt();
            if (age < 10 || age > 20) {
                System.out.println("您输入的年龄有误。");
            } else {
                break;
            }
        }
        String birthday;
        while (true) {
            System.out.println("请输入学生生日(如20050101)：");
            birthday = sc.next();
            if (birthday.length() != 8) {
                System.out.println("您输入的生日有误。");
            } else {
                break;
            }
        }
        Student stu = new Student(sid, name, age, birthday);
        list.add(stu);
        System.out.println("添加成功！");
    }

    //  删除学生 deleteStudent(list);
    public static void deleteStudent(ArrayList<Student> list) {
        lookStudent(list);
        Scanner sc = new Scanner(System.in);
//        lo:
//        while (true) {
        System.out.println("请输入学生学号(如：001)：");
        String sid = "itheima" + sc.next();
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (sid.equals(stu.getSid())) {
                list.remove(i);
//                    break lo;
                System.out.println("删除成功！");
                return;
            }
        }
        System.out.println("您输入的学号不存在。");
//        }
    }

    //  修改学生
    public static void setStudent(ArrayList<Student> list) {
        lookStudent(list);
        Scanner sc = new Scanner(System.in);
//        lo:
//        while (true) {
        System.out.println("请输入学生学号(如：001)：");
        String sid = "itheima" + sc.next();
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (sid.equals(stu.getSid())) {
                setStudent01_sid(list, sid, i);
//                    break lo;
                return;
            }
        }
        System.out.println("您输入的学号不存在。");
//        }
    }

    //  查看所有学生
    public static void lookStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("当前暂无学生信息，请添加后查看。");
            return;
        }
        System.out.println("当前所有学生信息如下：");
        System.out.println("学号\t\t" + "姓名\t" + "年龄\t" + "生日\t");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.print(stu.getSid() + "\t" + stu.getName() + "\t");
            System.out.println(stu.getAge() + "\t" + stu.getBitrhday());
        }
    }

    //  根据sid修改学生信息
    public static void setStudent01_sid(ArrayList<Student> list, String sid, int i) {
        Scanner sc = new Scanner(System.in);
        Student stu = list.get(i);
        String name = stu.getName();
        int age = stu.getAge();
        String birthday = stu.getBitrhday();
        lo:
        while (true) {
            System.out.println("当前学生可修改信息如下：");
            System.out.println("1.学生姓名：" + name);
            System.out.println("2.学生年龄：" + age);
            System.out.println("3.学生生日：" + birthday);
            System.out.println("4.结束修改：");
            lo1:
            while (true) {
                System.out.println("请输入您的选择(1-4)：");
                int choice = sc.nextInt();
                switch (choice) {
                    case 1:
                        System.out.println("请输入学生姓名：");
                        name = sc.next();
                        break lo1;
                    case 2:
                        while (true) {
                            System.out.println("请输入学生年龄(10-20)：");
                            age = sc.nextInt();
                            if (age < 10 || age > 20) {
                                System.out.println("您输入的年龄有误。");
                            } else {
                                break lo1;
                            }
                        }
                    case 3:
                        while (true) {
                            System.out.println("请输入学生生日(如20050101)：");
                            birthday = sc.next();
                            if (birthday.length() != 8) {
                                System.out.println("您输入的生日有误。");
                            } else {
                                break lo1;
                            }
                        }
                    case 4:
                        System.out.println("修改成功，本次修改已完成。");
                        break lo;
                    default:
                        System.out.println("您输入的选项不存在");
                        break;
                }
            }
        }
        Student stu1 = new Student(sid, name, age, birthday);
        list.set(i, stu1);
    }

}
